/*******************************************************************************
 * Drag Drop	
 * Copyright (c) 2009 Mario C. Ponciano and others.
 * This program is free software; you can redistribute it and/or		
 * modify it under the terms of the GNU General Public License			
 * as published by the Free Software Foundation; either version 2		
 * of the License, or (at your option) any later version.
 *
 *Contributors:
 *Mario C. Ponciano - a.k.a Razec (mrazec@gmail.com) 
 *
 *******************************************************************************/

package org.skypilot.plugin.views;


import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.rmi.RemoteException;
import java.util.Map;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.xml.rpc.ServiceException;


import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ExtendedModifyEvent;
import org.eclipse.swt.custom.ExtendedModifyListener;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyAdapter;
import org.skypilotplugin.util.ConsumingService;

public class DashboardView extends ViewPart {

	private Composite parent;  

	public void createPartControl(Composite parent) {
		Composite contents = new Composite(parent, SWT.NONE);
		contents.setLayout(new GridLayout(1,false));
		this.parent = contents;

		new Label(contents, SWT.NONE).setText("Add Todo:");

		Composite inputComposite = new Composite(contents, SWT.NONE);
		inputComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		inputComposite.setLayout(new GridLayout(2,false));

		final Text inputText = new Text(inputComposite, SWT.BORDER);
		inputText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		Button submit = new Button(inputComposite,SWT.PUSH | SWT.BORDER);
		submit.setText("Add!");
		submit.setLayoutData(new GridData());
		new Label(contents, SWT.NONE).setText("Dashboard Information:");
		createGUI(parent);
	}
	//Post-it create
	private void createGUI(Composite parent){
		parent.setLayout(new GridLayout(1, false));
		int style = SWT.MULTI | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER;
		final ConsumingService csrv = new ConsumingService();
		String[] query = csrv.selectText("Text");
		String[] nID = csrv.selectText("ID");
		for(int i =0;i< csrv.selectQTDPOSTIT();i++){
			final int id;
			final StyledText text1 = new StyledText(parent, style);
			GridData gridData = new GridData(GridData.FILL_BOTH);
			gridData.horizontalSpan = 2;
			Device display = null;
			Color light_yellow = new Color(display, 255, 255, 204);
			//text1.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_YELLOW));
			text1.setBackground(light_yellow);
			//This event modify all text.
			id = i;
			System.out.println(nID[i]);
			text1.addExtendedModifyListener(new ExtendedModifyListener() {
			      public void modifyText(ExtendedModifyEvent event) {
			    	  csrv.editText("snippet",text1.getText(),Integer.toString(id));
			        }
			      });
			text1.setLayoutData(gridData);
			text1.setText(query[i]);//show the values
			System.out.println(query[i]);
			//Move the text to other window.
			DragSource source = new DragSource(text1, DND.DROP_COPY | DND.DROP_MOVE);
			source.setTransfer(new Transfer[] {TextTransfer.getInstance()});
			source.addDragListener(new DragSourceAdapter() {
				Point selection;
				public void dragStart(DragSourceEvent e) {
					selection = text1.getSelection();
					e.doit = selection.x != selection.y;
				}
				public void dragSetData(DragSourceEvent e) {
					e.data = text1.getText(selection.x, selection.y-1);
				}
				public void dragFinished(DragSourceEvent e) {
					if (e.detail == DND.DROP_MOVE) {
						text1.replaceTextRange(selection.x, selection.y - selection.x, "");
					}
					selection = null;
				}
			});		
		}

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}


}